<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Shopping_list extends CI_Controller {
    public function index()
    {
        $data['title'] = "Shopping List";        
        $data['data'] = array ('response'=>$this->session->flashdata('message'));
        $data['js'] = array('shopping_list.js');
        $data['css'] = array('shopping_list.css','datepicker/datepicker.min.css');
        $data['view'] = "stats/view_shopping_list";
        $this->load->view('view_template',$data);
    }
    
    public function pdf(){
        $from   = $this->input->post('from_date');
        $to     = $this->input->post('to_date');
        $meal =     $this->input->post('meal');
        $ingredient =     $this->input->post('ingredient');
        
        if ($meal){
            $this->meals($from, $to);
            return;
        }
        else if ($ingredient){
            $this->ingredients($from, $to);
            return;
        }
        
        $this->index();
        
    }
    
    private function ingredients($from, $to){
        
        $ingredients = array();
        
        try{
            $this->load->model('statistics');
            $ingredients = $this->statistics->getIngredientsOnRange($from,$to);
        }
        catch(Exception $e){
            $this->index;
        }
        
        $this->load->library('pdf', array('P','mm','A4'));

        $this->pdf->setTextTitle("Ingredient Shopping List:","Between $from and $to:");
        $this->pdf->setDrawColPos();
        $this->pdf->setTextTitle("Ingredient Shopping List cont...","");
        foreach($ingredients->result() as $result){
            
            $this->pdf->setTextColor(50,50,50);
            $this->pdf->Cell(50,5, '[   ] '.$result->ingredient_title, 0 ,0);
            $this->pdf->SetTextColor(150,180,200);
            $this->pdf->Cell(5,5, 'x'.$result->ingredient_count, 0 ,1);
            
            $this->pdf->setDrawColPos();
        }
        
        $this->pdf->Output();
    }
    
    private function meals($from, $to){
        
        $ingredients = array();
        
        try{
            $this->load->model('statistics');
            $meals = $this->statistics->getMealVotesOnRange($from,$to);
        }
        catch(Exception $e){
            $this->index;
        }
        
        $this->load->library('pdf', array('P','mm','A4'));
        $this->pdf->AddPage();
        $this->pdf->setTextTitle("Average Meal Votes","For meals between $from and $to:");
        $this->pdf->printTitle();
        foreach($meals as $meal){
            $this->pdf->drawMealCell($meal);
        }
        
        $this->pdf->Output();
    }
 
}

